define(['jquery', 'bbcare',
    'scprit/followup/app/pumpBreastStatistics/pumpBreastStatistics.servers.js'
],
    function ($, $bb, $server) {
        var pump = {
            table: {
                current: 1,
                pageSize: 10,
                total: 1,
                queueId: '',
                patientName: '',
                hospitalNumber: '',
                motherName: '',
                gender: ''
            }
        }

        /**
         * 获取到列表之后的回调
         * @param { object } result 
         */
        var getpumpBreastRecordListCallback = function (result) {
            pump.table.total = Math.ceil(result.data.count / 10)
            renderTabel(result.data.data)
        }

        /**
         * 输入住院号, 姓名， 母亲姓名时
         */
        var input_timer = undefined
        var handleInput = function () {
            var type = $(this).data('type')
            pump.table[type] = $(this).val()
            input_timer && clearTimeout(input_timer)
            input_timer = setTimeout(function () {
                pump.table.current = 1
                getList()
            }, 800)
        }

        /**
         * 选择下拉框
         */
        var handleSelect = function () {
            pump.table.gender = $(this).val()
            pump.table.current = 1
            getList()
        }

        /**
         * 获取记录列表
         * @param {*} currentPage   当前页码
         * @param {*} pageSize      当前页的条数
         * @param {*} patientId     患者 ID
         */
        var getList = function () {
            var params = {
                currentPage: pump.table.current,
                pageSize: pump.table.pageSize,
                queueId: pump.table.queueId,
                patientName: pump.table.patientName,
                hospitalNumber: pump.table.hospitalNumber,
                motherName: pump.table.motherName,
                gender: getGender(pump.table.gender)
            }
            loading('加载中...')
            $server.getpumpBreastRecordList(params, getpumpBreastRecordListCallback)
        }

        /**
         * 判断当前用户是男是女
         * @param { string } gender 性别 
         */
        var getGender = function (gender) {
            switch (gender) {
                case 'N':
                    return 'N'
                case 'F':
                    return 'F'
                case '男':
                    return 'N'
                case '女':
                    return 'F'
                default:
                    return ''
            }
        }


        /**
         * 点击翻页按钮
         */
        var handleClickPageBtn = function () {
            var type = $(this).data('type')
            switch (type) {
                case 'prev':
                    if (pump.table.current === 1) return
                    pump.table.current--
                    break
                case 'next':
                    if (pump.table.current === pump.table.total) return
                    pump.table.current++
                    break
                case 'jump':
                    var val = Number($('._pageCtrl_input').val())
                    // 如果输入的不是数字时
                    if (val !== val) {
                        return $('._pageCtrl_input').val(pump.table.current)
                    }
                    if (val > pump.table.total) {
                        pump.table.current = pump.table.total
                    } else {
                        pump.table.current = val
                    }
                    break
            }
            $('._pageCtrl_input').val(pump.table.current)
            getList()
        }

        /**
         * 初始化表格
         */
        var renderTabel = function (data) {
            $('.created').remove()
            $('._pumpBreast_table_none').hide()
            var html = ''
            for (var i = 0; i < data.length; i++) {
                var gender = data[i].Pub1100003 === 'N' || data[i].Pub1100003 === '男' ? '男' : '女'
                var zyh = data[i].Pub12x00008 ? data[i].Pub12x00008 : '无'
                var ch = data[i].zcejd_ch ? data[i].zcejd_ch : '无'
                var mqxm = data[i].zcejd_mqxm ? data[i].zcejd_mqxm : '无'
                var brcs = data[i]['24hourTimes'] ? data[i]['24hourTimes'] : '无'
                var zbrl = data[i]['24hourQuantity'] ? data[i]['24hourQuantity'] : '无'
                var scmr = data[i]['zcejd_scmrsj'] ? data[i]['zcejd_scmrsj'] : '无'
                html += '<tr class="created">' +
                    '<td>' + data[i].Pub1100002 + '</td>' +
                    '<td>' + gender + '</td>' +
                    '<td>' + zyh + '</td>' +
                    '<td>' + ch + '</td>' +
                    '<td>' + mqxm + '</td>' +
                    '<td>' + scmr + '</td>' +
                    '<td>' + brcs + '</td>' +
                    '<td>' + zbrl + '</td>' +
                    '<td><span class="_gotoInfo" data-id="' + data[i].patientId + '">查看泵乳统计</span></td>' +
                    '</tr>'
            }
            $('._pumpBreast_table').append(html)
            $('._pageCtrl_count').text(pump.table.total)
            $('._pageCtrl_input').val(pump.table.current)
            removeLoading('test')
            if (html === '') $('._pumpBreast_table_none').show()
        }

        pump.init = function () {
            var queueId = ''
            try {
                queueId = JSON.parse(window._config).followQueueId
            } catch (err) {
                queueId = ''
                $bb.toastShow('队列 ID 获取失败')
            }
            pump.table.queueId = queueId
            // 绑定换页按钮
            $('._btns_item').click(handleClickPageBtn)
            // 输入框
            $('._pumpBreast_limit_block_input').keyup(handleInput)
            // 选择框
            $('._pumpBreast_limit_block_select').change(handleSelect)
            // 获取数据
            pump.table.current = 1
            pump.table.patientName = ''
            pump.table.hospitalNumber = ''
            pump.table.motherName = ''
            pump.table.gender = ''
            getList()
        }

        $('.content-main').on('click', '._gotoInfo', function () {
            var data = $(this).data()
            console.log(data)
            $bb.openPage('pumpBreastDetail', data)
        })

        function loading (text) {
            $('body').loading({
                loadingWidth: 240,
                title: '请稍等!',
                name: 'test',
                discription: text,
                direction: 'column',
                type: 'origin',
                // originBg:'#71EA71',
                originDivWidth: 40,
                originDivHeight: 40,
                originWidth: 6,
                originHeight: 6,
                smallLoading: false,
                loadingMaskBg: 'rgba(0,0,0,0.2)'
            })
        }

        return pump

    })